package com.systop.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rabbitmq.client.Channel;
import com.systop.entity.GoodsStock;
import com.systop.entity.Order;
import com.systop.entity.StockLockDetail;
import com.systop.service.GoodsStockService;
import com.systop.service.OrderService;
import com.systop.service.StockLockDetailService;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.ChannelAwareMessageListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
// import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @Description: TODO
 * @Author: jsj
 * @Date: 2022/6/17
 */


// @RabbitListener(queues = "stock.release.stock.queue")
@Component
public class StockReleaseListener implements ChannelAwareMessageListener {

    @Autowired
    StockLockDetailService stockLockDetailService;

    @Autowired
    OrderService orderService;

    @Autowired
    GoodsStockService goodsStockService;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        System.out.println("收到解锁库存的消息");
        try {
            // 使用jackson解析
            StockLockDetail stockLockDetail = JSON.parseObject(new String(message.getBody()), StockLockDetail.class);
            //查询所有订单
            List<Order> orderList = orderService.list(new QueryWrapper<Order>().eq("order_no", stockLockDetail.getOrderNo()));
            if (orderList!=null && orderList.size()>0) {
                long count = orderList.stream().filter(order -> {
                    //判断订单是否处于已取消状态
                    if (order.getOrderStatus() == 2) {
                        return true;
                    }
                    return false;
                }).count();
                //该订单号下的所有订单状态都处于已取消情况下继续执行
                if(orderList.size()==count){
                    //查询所有库存详情
                    List<StockLockDetail> stockLockDetailList = stockLockDetailService.list(
                            new QueryWrapper<StockLockDetail>().eq("order_no", stockLockDetail.getOrderNo()));
                    if(stockLockDetailList!=null && stockLockDetailList.size()>0){
                        goodsStockService.unLockStock(stockLockDetailList);
                        //消费成功后将手动确认消息
                        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    }
                }else if(orderList.stream().filter(order->{
                    return order.getOrderStatus() == 1;
                }).count()==orderList.size()){
                    //消费成功后将手动确认消息
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                }
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            }
            //消费成功后将手动确认消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            //如果发送失败，消息会重新进入队列，再次被这个消费者消费
            //消息发送失败后，也可以将消息发送到别的队列，让其他消费者进行消费
            //第三个参数 true为重新将消息放入队列，如果设置为false，则抛弃这条消息
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }
}
